import React from 'react';
import './IndexGoodsCar.less';
import { withRouter } from 'react-router-dom';
import {reqCartadd} from '../../../http/api';
import { successAlert } from '../../../utils/alert';

async  function addShopCar(e,goodsid){
    e.stopPropagation();
    let params = {
        uid:JSON.parse(localStorage.getItem('userInfo')).uid,
        type:1,
        goodsid,
        num:1
    }
    try{
        let res = await reqCartadd(params);  
        if(res.data.code === 200){
            successAlert(res.data.msg)
        }
    }catch(err){
        console.log(err);
    }
                       
}

const Indexgoodscar = (props) => {
    let indexgoodsArr = props.indexgoodsArr;
                       
  return (
    <div className='Indexgoodscar'>
        {indexgoodsArr.map((item,index)=>{
            return <ul key={index}>
                {item.content.map(i=>{
                    return <li key={i.id} onClick={()=>props.history.push(`/detail/${i.id}`)}>
                    <div className="left">
                        <img src={i.img} alt="" />
                    </div>
                    <div className="right">
                        <h3>{i.goodsname}</h3>
                        <div className="pic">
                        <img src="/uploads/提示标签@3x.png" alt="" />
                        <span>直降198</span><span>限购一件</span>
                        </div>
                        <div className='footer'>
                            <div className="price">
                                <span>￥{i.price}</span>
                                <del>￥{i.market_price}</del>
                            </div>
                            <button onClick={(e)=>addShopCar(e,i.id)}><img src="/uploads/加购@3x.png" alt="" /></button>
                        </div>
                    </div>
                </li>
                })}
            </ul>
        })}
    </div>
  )
}

export default withRouter(Indexgoodscar);
